/**
 * This class is responsible for writing log entries for major errors and for
 * general debug statements.
 *
 * @author Jeremiah Weeden
 * @version 4/27/2007
 **/

package com.elderhealth.util;

import java.lang.String;
import java.util.Date;
import java.util.Properties;

import java.io.BufferedWriter;
import java.io.FileWriter;

public class Log
{
   /* Instance Variables */
   public final Properties properties;
   public final String logFile;
   public final String debugOn;
   public final String printToScreen;
   private Date current;
   private static Log instance;
   private static boolean wasCreatedSuccessfully;

   /**
    * Constructor for objects of class Log
    */
   private Log()
   {
     properties = ElderhealthProperties.getInstance();
     if (!properties.isEmpty())
     {
       logFile = properties.getProperty("LOG_FILE_LOCATION");
       debugOn = properties.getProperty("DEBUG_ON");
       printToScreen = properties.getProperty("PRINT_TO_SCREEN");
       wasCreatedSuccessfully = true;
     }
     else
     {
       logFile = "";
       debugOn = "";
       printToScreen = "";
       wasCreatedSuccessfully = false;
     }
   }

   /* Static Method Section */

   /**
    * Gets the value of the attribute: instance.
    *
    * @return    Log
    **/
   public static Log getInstance()
   {
      if (null == instance)
      {
        instance = new Log();
      }
      return instance;
   }

   /* Method Section */

   /**
    * editLog is a private function that writes directly to the log file.
    * It is private as to prevent users from directly writing to the log
    * file.
    *
    * @param     toWrite
    **/
   private void editLog(String toWrite)
   {
     /* Attempt to write to the log file */
     try
     {
       if (wasCreatedSuccessfully)
       {
         /* Open up a file writer and write the message to the log file */
         BufferedWriter in = new BufferedWriter(new FileWriter(logFile, true));
         in.write(toWrite);
         in.newLine();
         in.close();
         if (null != printToScreen && printToScreen.equals("true"))
         {
           System.out.println(toWrite);
         }
       }
       else
       {
         System.out.println(toWrite);
       }
     }
     catch (Exception fnf)
     {
       /* If the log file cannot be found, print the output to the screen */
       System.out.println("Log : Log file not found. " +
           "Message to write was: " + toWrite);
     }
   }

   /**
    * error will call editLog to write String errorMessage to the log
    *
    * @param     errorMessage
    **/
   public void error(String errorMessage)
   {
     try
     {
       /* Add the error message to the log with the current timestamp */
       current = new Date();
       String logMessage = new String(current.toString() + "::: Error: "
           + errorMessage);
       editLog(logMessage);
     }
     catch (java.lang.NullPointerException ne)
     {
       /* NullPointer exception, print the output to the screen */
       System.out.println("Log : error: NullPointerException: Check earlier " +
           "for errors.");
       System.out.println("Log : error: Properties most likely not loaded.");
       System.out.println("Log : error: Message to write was: " +
           errorMessage);
     }
     catch (Exception e)
     {
       /* Error occurred above, print the output to the screen */
       System.out.println("Log : Exception occurred, error was: " +
           e.toString() + "|| Message to write to log was: " + errorMessage);
     }
   }

   /**
    * debug will call editLog to write String debugMessage to the log if
    * debugOn is true (debugOn is a property in the properties file)
    *
    * @param     debugMessage
    **/
   public void debug(String debugMessage)
   {
     try
     {
       /* Only add the message to the log if currently in debug mode */
       if (null != debugOn && debugOn.toLowerCase().equals("true"))
       {
         /* Add the message to the log with the current timestamp */
         current = new Date();
         String logMessage = new String(current.toString() + "::: Debug: "
             + debugMessage);
         editLog(logMessage);
       }
     }
     catch (java.lang.NullPointerException ne)
     {
       /* NullPointer exception, print the output to the screen */
       System.out.println("Log : error: NullPointerException: Check earlier " +
         "for errors.");
       System.out.println("Log : error: Properties most likely not loaded.");
       System.out.println("Log : error: Message to write was: " +
           debugMessage);
     }
     catch (Exception e)
     {
       /* Error occurred above, print the output to the screen */
       System.out.println("Log : Exception occurred, error was: " +
           e.toString() + "|| Message to write to log was: " + debugMessage);
     }
   }
}